package com.lanxin.oa.message.service;

import com.lanxin.bean.XxDuanxiaoxi;
import com.lanxin.bean.XxXiaoxijieshou;
import com.lanxin.bean.ZzAccount;
import com.lanxin.oa.message.dao.DuanxiaoxiDao;
import com.lanxin.util.PageBean;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class DuanxiaoxiServiceImpl implements DuanxiaoxiService {

    @Autowired
    private DuanxiaoxiDao duanxiaoxiDao;

    @Override
    public PageBean<XxDuanxiaoxi> listDuanxiaoxi(Integer page, Integer pageSize, Integer creator) {
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 3;
        }
        return duanxiaoxiDao.listDaunxiaoxi(page, pageSize, creator);
    }

    @Override
    public void insertDuanxiaoxi(HttpServletRequest request, XxDuanxiaoxi duanxiaoxi, Integer[] empids, MultipartFile[] file) {
        ZzAccount account = (ZzAccount) request.getSession().getAttribute("account");
        if (empids != null && empids.length > 0) {
            for (int i = 0; i < empids.length; i++) {
                XxXiaoxijieshou xiaoxijieshou = new XxXiaoxijieshou();
                xiaoxijieshou.setJieshouren(empids[i]);
                System.out.println(empids[0]);
                xiaoxijieshou.setXiaoxiid(duanxiaoxi.getOid());
                xiaoxijieshou.setIsread("0");
                duanxiaoxi.getXiaoxijieshou().add(xiaoxijieshou);
            }
        }
        if (file != null && file.length > 0) {
            String str = upload(request, file);
            duanxiaoxi.setFiles(str);
        }
        duanxiaoxi.setCreator(account.getEmployee().getOid());
        duanxiaoxi.setCreateDate(new Date());
        duanxiaoxiDao.add(duanxiaoxi);
    }

    @Override
    public PageBean<XxDuanxiaoxi> pageDaunxiaoxi(Integer page, Integer pageSize, Integer creator, String biaoti,
                                                 Integer jieshouren, Date startDate, Date endDate) {
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 3;
        }
        return duanxiaoxiDao.pageDuanxiaoxi(page, pageSize, creator, biaoti, jieshouren, startDate, endDate);
    }

    @Override
    public PageBean<XxXiaoxijieshou> xxjsList(Integer page, Integer pageSize, Integer jieshouren) {
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 3;
        }
        return duanxiaoxiDao.xiaoxijieshou(page, pageSize, jieshouren);
    }

    @Override
    public PageBean<XxXiaoxijieshou> pageXxjs(Integer page, Integer pageSize, String biaoti,
                                              Integer jieshouren, Date startDate, Date endDate, Integer jieshourens) {
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 3;
        }
        return duanxiaoxiDao.pageDuanxiaoxi(page, pageSize, biaoti, jieshouren, startDate, endDate, jieshourens);
    }

    @Override
    public XxXiaoxijieshou xxjsxq(Integer oid) {
        XxXiaoxijieshou xiaoxijieshou = duanxiaoxiDao.xxjsxq(oid);
        if (xiaoxijieshou.getYuedushijian()==null) {
            System.out.println(xiaoxijieshou.getIsread());
            xiaoxijieshou.setIsread("1");
            xiaoxijieshou.setYuedushijian(new Date());
            duanxiaoxiDao.update(xiaoxijieshou);
        }
        return xiaoxijieshou;
    }

    @Override
    public void xiazaifiles(String fileName, HttpServletResponse response, HttpServletRequest request) {
        String dir = request.getServletContext().getRealPath("upload");
        String str = fileName.substring(fileName.lastIndexOf("/") + 1);
        try {
            str = URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        File file = new File(dir + "/" + str);
        response.reset();
        response.setHeader("Content-Disposition", "attachment;filename=\"" + str + "\"");
        response.addHeader("Content-Length", file.length() + "");
        response.setContentType("application/octet-stream;charset=utf-8");
        try {
            System.out.println(file);
            response.getOutputStream();
            FileUtils.copyFile(file, response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void xiazai(String fileName, HttpServletResponse response, HttpServletRequest request) {
        xiazaifiles(fileName, response, request);
    }

    //文件上传
    public String upload(HttpServletRequest request, MultipartFile[] file) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dir = request.getServletContext().getRealPath("upload");
        System.out.println(dir);
        for (int j = 0; j < file.length; j++) {
            if (file[j].getSize() > 0) {
                String date = sdf.format(new Date());
                File le = new File(dir + "/" + date);
                le.mkdir();
                str = date + "/" + file[j].getOriginalFilename();
                File newfile = new File(le, file[j].getOriginalFilename());
                try {
                    FileUtils.copyInputStreamToFile(file[j].getInputStream(), newfile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return str;
    }
}
